
using AutoMapper;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Model;
using SqlSugar;
using Util;

namespace API.Controllers
{
    public class DoorRecordController : BaseController
    {
        Repository<tDoorRecord> _DoorRecord;
        readonly IMapper _mapper;
        public DoorRecordController(Repository<tDoorRecord> DoorRecord, IMapper mapper)
        {
            _DoorRecord = DoorRecord;
            _mapper = mapper;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultMsg<string>> Save([FromBody] SaveDoorRecordInput dto)
        {
            ResultMsg<string> result = new ResultMsg<string>();
            try
            {
                var model = _mapper.Map<tDoorRecord>(dto);
                InitEntity(model);
                await _DoorRecord.InsertOrUpdateAsync(model);
                return result.Success();
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("DoorRecordController--Save:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultMsg<List<DoorRecordOutput>>> GetList([FromBody] DoorRecordListInput dto)
        {
            ResultMsg<List<DoorRecordOutput>> result = new ResultMsg<List<DoorRecordOutput>>();
            try
            {
                if (dto.PageNum <= 0)
                {
                    return result.Error("PageNum不能小于或等于0");
                }
                if (dto.PageSize <= 0)
                {
                    return result.Error("PageSize不能小于或等于0");
                }
                SqlSugar.PageModel pageModel = new SqlSugar.PageModel
                {
                    PageIndex = dto.PageNum,
                    PageSize = dto.PageSize,
                };
                var exp = Expressionable.Create<tDoorRecord>();
                //if (!string.IsNullOrWhiteSpace(dto.Name))
                //{
                //    exp.And(x => x.FName.Contains(dto.Name));
                //}
                var list = (await _DoorRecord.GetPageListAsync(exp.ToExpression(), pageModel)).OrderByDescending(x => x.CreateTime).ToList();
                var DoorRecordOutputs = _mapper.Map<List<DoorRecordOutput>>(list);
                return result.Success(DoorRecordOutputs, pageModel.TotalCount);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("DoorRecordController--GetList:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 获取唯一
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultMsg<DoorRecordOutput>> GetTheOne(string id)
        {
            ResultMsg<DoorRecordOutput> result = new ResultMsg<DoorRecordOutput>();
            try
            {
                var model = await _DoorRecord.GetByIdAsync(id);
                var dao = _mapper.Map<DoorRecordOutput>(model);
                return result.Success(dao);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("DoorRecordController--GetTheOne:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultMsg<string>> Del(string id)
        {
            ResultMsg<string> result = new ResultMsg<string>();
            try
            {
                await _DoorRecord.DeleteByIdAsync(id);
                return result.Success();
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("DoorRecordController--Del:" + ex.ToString());
                return result.Error(ex.ToString());
            }
        }
    }
}
